﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Diagnostics;
using System.ComponentModel;
using System.Windows.Data;
using System.Windows;

namespace Freshome.m.vms
{
    public class MainWindow : INotifyPropertyChanged
    {
        private ObservableCollection<TabItemate> _wks = new ObservableCollection<TabItemate>();

        public MainWindow()
        {
            _wks.Add(new Firstab("主页") { });
            _wks.CollectionChanged += this.OnWorkspacesChanged;
        }

        #region Workspaces

        /// <summary>
        /// Returns the collection of available workspaces to display. A 'workspace' is a ViewModel that can request to be closed.
        /// </summary>
        public ObservableCollection<TabItemate> Workspaces
        {
            get { return _wks; }
        }

        void OnWorkspacesChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null && e.NewItems.Count != 0)
            {
                foreach (TabItemate wk in e.NewItems) wk.CloseRequested += this.OnWorkspaceRequestClose;
            }
            if (e.OldItems != null && e.OldItems.Count != 0)
            {
                foreach (TabItemate wk in e.OldItems) wk.CloseRequested -= this.OnWorkspaceRequestClose;
            }
        }
        void OnWorkspaceRequestClose(object sender, EventArgs e)
        {
            TabItemate wk = sender as TabItemate;
            this.Workspaces.Remove(wk);
        }

        #endregion // Workspaces

        #region Commands

        private Magicommand _majorawcmd;
        public Magicommand MajorawCmd
        {
            get
            {
                if (_majorawcmd == null)
                {
                    _majorawcmd = new Magicommand(Properties.Resources.Majoraw + "(_J)", param => this.ShowMajoraw());
                }
                return _majorawcmd;
            }
        }
        private void ShowMajoraw()
        {
            if (m_Userole != null)
            {
                string nm = Properties.Resources.Majoraw;
                var wk = this._wks.FirstOrDefault(w => w.Name == nm);
                if (wk == null)
                {
                    wk = new Majoraws(nm, _ur);
                    wk.Loadata();
                    this.Workspaces.Add(wk);
                }
                this.ActivateWorkspace(wk);
            }
        }
        private Magicommand _minorawcmd;
        public Magicommand MinorawCmd
        {
            get
            {
                if (_minorawcmd == null)
                {
                    _minorawcmd = new Magicommand(Properties.Resources.Minoraw + "(_I)", param => this.ShowMinoraw());
                }
                return _minorawcmd;
            }
        }
        private void ShowMinoraw()
        {
            if (m_Userole != null)
            {
                string nm = Properties.Resources.Minoraw;
                var wk = this._wks.FirstOrDefault(w => w.Name == nm);
                if (wk == null)
                {
                    wk = new Minoraws(nm, _ur);
                    wk.Loadata();
                    this.Workspaces.Add(wk);
                }
                this.ActivateWorkspace(wk);
            }
        }
        private Magicommand _healthcmd;
        public Magicommand HealthCmd
        {
            get
            {
                if (_healthcmd == null)
                {
                    _healthcmd = new Magicommand(Properties.Resources.Health + "(_H)", param => this.ShowHealth());
                }
                return _healthcmd;
            }
        }
        private void ShowHealth()
        {
            if (m_Userole != null)
            {
                string nm = Properties.Resources.Health;
                var wk = this._wks.FirstOrDefault(w => w.Name == nm);
                if (wk == null)
                {
                    wk = new Healtholds(nm, _ur);
                    wk.Loadata();
                    this.Workspaces.Add(wk);
                }
                this.ActivateWorkspace(wk);
            }
        }

        // Recipes
        private Magicommand _coldishcmd;
        public Magicommand ColdishCmd
        {
            get
            {
                if (_coldishcmd == null)
                {
                    _coldishcmd = new Magicommand(Properties.Resources.Coldish + "(_D)", param => this.ShowRecipes(Rmt.RecipeCateg.Coldish));
                }
                return _coldishcmd;
            }
        }
        private Magicommand _dishotcmd;
        public Magicommand DishotCmd
        {
            get
            {
                if (_dishotcmd == null)
                {
                    _dishotcmd = new Magicommand(Properties.Resources.Dishot + "(_H)", param => this.ShowRecipes(Rmt.RecipeCateg.Dishot));
                }
                return _dishotcmd;
            }
        }
        private Magicommand _soupotcmd;
        public Magicommand SoupotCmd
        {
            get
            {
                if (_soupotcmd == null)
                {
                    _soupotcmd = new Magicommand(Properties.Resources.Soupot + "(_S)", param => this.ShowRecipes(Rmt.RecipeCateg.Soupot));
                }
                return _soupotcmd;
            }
        }
        private Magicommand _snackcmd;
        public Magicommand SnackCmd
        {
            get
            {
                if (_snackcmd == null)
                {
                    _snackcmd = new Magicommand(Properties.Resources.Snack + "(_N)", param => this.ShowRecipes(Rmt.RecipeCateg.Snack));
                }
                return _snackcmd;
            }
        }
        private Magicommand _pastrycmd;
        public Magicommand PastryCmd
        {
            get
            {
                if (_pastrycmd == null)
                {
                    _pastrycmd = new Magicommand(Properties.Resources.Pastry + "(_P)", param => this.ShowRecipes(Rmt.RecipeCateg.Pastry));
                }
                return _pastrycmd;
            }
        }
        private Magicommand _drinkcmd;
        public Magicommand DrinkCmd
        {
            get
            {
                if (_drinkcmd == null)
                {
                    _drinkcmd = new Magicommand(Properties.Resources.Drink + "(_K)", param => this.ShowRecipes(Rmt.RecipeCateg.Drink));
                }
                return _drinkcmd;
            }
        }
        private void ShowRecipes(Rmt.RecipeCateg ct)
        {
            if (m_Userole != null)
            {
                string nm = Properties.Resources.ResourceManager.GetString(ct.ToString());
                var wk = this._wks.FirstOrDefault(w => w.Name == nm);
                if (wk == null)
                {
                    wk = new Recipes(ct, nm, _ur);
                    wk.Loadata();
                    this.Workspaces.Add(wk);
                }
                this.ActivateWorkspace(wk);
            }
        }

        #endregion // Commands

        private void ActivateWorkspace(TabItemate wk)
        {
            Debug.Assert(this.Workspaces.Contains(wk));

            ICollectionView cv = CollectionViewSource.GetDefaultView(this.Workspaces);
            if (cv != null) cv.MoveCurrentTo(wk);
        }
        private string _leftext, _rightext;
        public string Leftext
        {
            get { return _leftext; }
            set
            {
                if (value != _leftext)
                {
                    _leftext = value;
                    OnPropertyChanged("Leftext");
                }
            }
        }
        public string Rightext
        {
            get { return _rightext; }
            set
            {
                if (value != _rightext)
                {
                    _rightext = value;
                    OnPropertyChanged("Rightext");
                }
            }
        }

        private fox.Userole _ur = null;
        private fox.Userole m_Userole
        {
            get
            {
                if (_ur == null)
                {
                    vws.UserInfox dlg = new vws.UserInfox();
                    dlg.Owner = Application.Current.MainWindow;
                    if (dlg.ShowDialog() == true)
                    {
                        if (dlg.Userole != null)
                        {
                            _ur = dlg.Userole;
                            if (_ur.IsSenior) Rightext = string.Format("{0}(管理)", _ur.UserName);
                            else if (_ur.IsJunior) Rightext = string.Format("{0}(编辑)", _ur.UserName);
                            else Rightext = string.Format("{0}(读取)", _ur.UserName);
                        }
                    }
                }
                return _ur;
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
